﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Forecast.Domain.Implementation
{
    public class AcceptBuyOfferService : IAcceptOffers
    {
        #region Interface

        public void Execute(IPlay acceptor, IOffer offer)
        {
            TransferStocks(acceptor, offer);
            TransferMoney(acceptor, offer);
        }

        #endregion
        #region Helper methods

        private void TransferStocks(IPlay me, IOffer offer)
        {
            ICountStocks holding = me.Portfolio.GetHolding(offer.Prediction);
            if (holding == Holding.NULL)
                holding = me.Portfolio.AddHolding(offer.Prediction);

            holding.AddStocks(offer.NumStocks);
            offer.Creator.Portfolio.GetHolding(offer.Prediction).RemoveStocks(offer.NumStocks);
        }

        private void TransferMoney(IPlay me, IOffer offer)
        {
            me.Wallet.RemoveCash(offer.TotalPrice);
            offer.Creator.Wallet.AddCash(offer.TotalPrice);
        }

        #endregion     
    }
}
